<?php

class Okute_Util_Data
{
    private $_session;
    private $_view;
    private $_severity_opts = array(0 => '至急',
                                    1 => '一週間以内',
                                    2 => '二週間以内',
                                    3 => '一ヶ月以内');

    public function __construct($view) {
        $this->_session = new Zend_Session_Namespace('okute');
        $this->_view = $view;
    }

    public function postToData($request, $data) {

        // charset => from charset
        $columns = array('req_id','req_content','req_amount','addressee_notes','severity','notes');
        foreach ($columns as $column) {
            $val = $request->getPost($column);
            if ($val !== null) {
                $data->$column = $this->convertInputEncoding($val);
            }
        }
        return $data;
    }

    public function dataToView($data) {
        $this->_view->data = $data->toArray();
        $this->_view->severity_opts = $this->_severity_opts;
        $this->_view->severity_opt = $this->_severity_opts[$data->severity];
    }

    public function dataToSession($data) {
        $this->_session->userData = $data->toArray();
    }

    public function clearSessionData() {
        $this->_session->userData = null;
        //$this->_session->requestMap = null;
        $this->_session->reqId = null;
        $this->_session->shelterMap = null;
        $this->_session->shelterId = null;
        $this->_session->confirm = null;
        $this->_session->page = null;
        $this->_session->displayId = null;
        $this->_session->filterStatus = null;
        $this->_session->filterOrder = null;
    }

    public static function convertInputEncoding($input) {
        $conversionTable = array(
            'SJIS'      => 'SJIS-win',
            'Shift_JIS' => 'SJIS-win',
            'EUC-JP'    => 'eucJP-win',
            'UTF-8'     => 'UTF-8');

        $charset = Zend_Registry::get('charset');
        $output = null;
        if ($conversionTable[$charset]) {
            $output = mb_convert_encoding($input, 'UTF-8', $conversionTable[$charset]);
        }
        return $output;
    }
}
